package com.xuecheng.ucenter.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.ucenter.mapper.XcMenuMapper;
import com.xuecheng.ucenter.mapper.XcUserMapper;
import com.xuecheng.ucenter.model.dto.AuthParamsDto;
import com.xuecheng.ucenter.model.dto.XcUserExt;
import com.xuecheng.ucenter.model.po.XcMenu;
import com.xuecheng.ucenter.model.po.XcUser;
import com.xuecheng.ucenter.service.AuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Hes
 * @description 配置用户信息服务
 * @date: 2023/6/25 22:57
 */
@Service
@Slf4j
public class UserServiceImpl implements UserDetailsService {

    @Autowired
    XcUserMapper xcUserMapper;

//    @Autowired
//    AuthService authService;

    @Autowired
    ApplicationContext applicationContext;

    @Autowired
    XcMenuMapper xcMenuMapper;

    /**
     * 根据账号查询用户信息
     *
     * @param s 账号
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        AuthParamsDto authParamsDto = null;
        authParamsDto = JSON.parseObject(s, AuthParamsDto.class);
        String authType = authParamsDto.getAuthType();
        AuthService authService = applicationContext.getBean(authType + "_authservice", AuthService.class);
        XcUserExt xcUserExt = authService.execute(authParamsDto);
        UserDetails userDetails = getUserDetails(xcUserExt);
        return userDetails;
    }

    /**
     * UserDetail
     *
     * @param xcUserExt
     * @return
     */
    private UserDetails getUserDetails(XcUserExt xcUserExt) {
        //取出数据库存储的正确密码
//        String password = xcUser.getPassword();
        String password = xcUserExt.getPassword();
        //设置权限
        //用户权限,如果不加报Cannot pass a null GrantedAuthority collection
        String id = xcUserExt.getId();
        List<XcMenu> xcMenus = xcMenuMapper.selectPermissionByUserId(id);
        ArrayList<String> permissions = new ArrayList<>();
        if (xcMenus.size() <= 0) {
            String[] authorities = {"p1"};
        } else {
            xcMenus.forEach(xcMenu -> {
                permissions.add(xcMenu.getCode());
            });
        }
        //将用户权限放在XcUserExt中
        xcUserExt.setPermissions(permissions);
        String[] authorities = permissions.toArray(new String[0]);
        //安全考虑，密码不能放在令牌中
        xcUserExt.setPassword(null);
        String userString = JSON.toJSONString(xcUserExt);
        UserDetails userDetails = User.withUsername(userString).authorities(authorities).password(password).build();
        return userDetails;
    }
}
